package cn.school.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.school.entity.Page;
import cn.school.entity.Student;
import cn.school.servies.StudentSerive;
import cn.school.tool.StringKit;

/**
 * Servlet implementation class JsonSearchStudent
 */
@WebServlet("/JsonSearchStudent")
public class JsonSearchStudent extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public JsonSearchStudent() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("application/json; charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		String x=null;
	
		String name=request.getParameter("name");
		name=StringKit.toNull(name);
		String qq=request.getParameter("qq");
		qq=StringKit.toNull(qq);
		String sta=request.getParameter("start");
		 sta=StringKit.toNull(sta);
		 String en=request.getParameter("end");
		 en=StringKit.toNull(en);	
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Date start=null;
		Date end=null;
		if(sta==""){
			 start=null;
		}else{
		 try {
			start= format.parse(sta);
		} catch (ParseException e) {

			e.printStackTrace();
		}
		}
		if(en==""){
			end=null;
		}else{
			 try {
				end= format.parse(en);
			} catch (ParseException e) {
			
				e.printStackTrace();
			}
		}
		boolean flag=true;
		if(start!=null&&end!=null){
		flag=start.before(end);
		}

		if(flag==false){
			Date time=start;
			start=end;
			end=time;
		}
		if(start!=null ){
		sta=format.format(start);
		}
		if(end !=null){
		 en=format.format(end);
		}

		StudentSerive ss=new StudentSerive();
		List<Student> list=null;
		if(name!=""&&qq!=""&&sta!=null&&en!=null){
		 list=ss.findAll();
		}else{
		 list=ss.show(name, qq, start, end);
		}
		int a=list.size();
		int pages=a/15+(a%15==0?0:1);
		String pagNo1=request.getParameter("pagNo");
		if(pagNo1==null){
			pagNo1="1";
		}
		int pagNo=Integer.parseInt(pagNo1);
	//	List<Student> list1=ss.showw(pagNo, 15, name, qq, start, end);
		        Page<Student>   p=  ss.paging(pagNo, name, qq, start, end);
//	String list2=	JSON.toJSONString(list1);
//String s="[{\"a\":"+a+"},{\"pagNo\":"+pagNo+"},{\"pages\":"+pages+"},{\"list2\":"+list2+"}]";
		        String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteNullStringAsEmpty,
						SerializerFeature.WriteMapNullValue);
response.getWriter().print(str);
//	request.getRequestDispatcher("servletjsp12-12/studentshow.jsp").forward(request, response);//;\  {\"name\":"+name +"},{\"qq\":"+qq+"}, ,{\"list1\":"+list1+"}

	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}
